package com.qef.com.mmovie.mvp.interactor;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.SignUpCallback;
import com.qef.com.mmovie.net.IGetDataDelegate;

import javax.inject.Inject;

/**
 * Created by feq on 2017/4/15.
 */

public class loginInteractor {
    @Inject
    public loginInteractor() {
    }

    public void login(String username, String password, IGetDataDelegate<AVUser> iGetDataDelegate) {
        AVUser.logInInBackground(username, password, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (e == null) {
                    iGetDataDelegate.getDataSuccess(avUser);
//                    Toast.makeText(App.getInstance(), "登录成功", Toast.LENGTH_SHORT).show();
//                    LoginActivity.this.finish();
//                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                } else {
                    iGetDataDelegate.getDataError(e);
//                    showProgress(false);
//                    Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    public void register(String username, String password, IGetDataDelegate<AVUser> iGetDataDelegate) {
        AVUser user = new AVUser();
        user.setUsername(username);
        user.setPassword(password);
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    iGetDataDelegate.getDataSuccess(AVUser.getCurrentUser());
                    // 注册成功，把用户对象赋值给当前用户 AVUser.getCurrentUser()
                    AVUser currentUser = AVUser.getCurrentUser();
//                    startActivity(new Intent(RegisterActivity.this, MainActivity.class));
//                    RegisterActivity.this.finish();
                } else {
                    iGetDataDelegate.getDataError(e);
                    // 失败的原因可能有多种，常见的是用户名已经存在。
//                    showProgress(false);
//                    Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
